if ("$kk" in window == false) {
    $kk = {};
    kk = $kk;
}
//配置
$kk.config = {
    loadingDelay: 800, //loading加载的延迟时间,800毫秒
    stopDrawing: false,//是否停止渲染选项
    jsonpTimeout: 10000, //jsonp超时时间,默认10秒
    loadingZindex : 10003,
    login: {
        url: "/OAuth/Login",     //当ajax请求返回4002未登录的时候跳转到指定地址
        target: window
    }
}

//原始数据
$kk.data = {
    icheck: {
        js: "http://static.tops001.com/bootstrap.icheck.min.js",
        checkboxClass: 'icheckbox_square',
        radioClass: 'iradio_square',
        increaseArea: '20%'
    },
    "switch": {
        js: "http://static.tops001.com/bootstrap-switch.min.js"
    }
};

$(function () {
    if ("dialog" in window == false) {
        $kk.include.apply(window, ["http://static.tops001.com/dialog-full-min.js"]);
    }
});

$kk.error = function (message) {
    $kk.popup(message, "error");
};

//触发事件
$kk.fireEvent = function (jq, event) {
    $(jq).each(function () {
        if (this.isFireEvent) {
            return;
        }
        this.isFireEvent = true;
        $(this).trigger(event);
        delete this.isFireEvent;
    });
};

//获取参数
$kk.getElementArgs = function (jq, option) {
    try {
        var args = $(jq).eq(0).attr("kk-data");
        if (args) {
            args = eval("(" + args + ")");
        }
        if ($.isPlainObject(args)) {
            return args;
        }
        if (option) {
            args = args || {};
            for (var i in option) {
                if (i in args == false) {
                    agrs[i] = option[i];
                }
            }
        }
        return args || {};
    } catch (e) {
        return option || {};
    }
};

//克隆对象
$kk.clonePlainObject = function (obj) {
    return $.extend({}, obj);
};

//合并对象,数值以obj1为主
$kk.mergePlainObject = function (obj1, obj2) {
    var args = $.merge([true, {}], arguments)
    return $.extend.apply($, args);
}

//分析方法参数
$kk.parseArgs = function () {
    var args = {
        Number: [],
        String: [],
        Boolean: [],
        Array: [],
        Json: [],
        DateTime: [],
        Object: [],
        Function: [],
        JQuery: [],
        Dom: []
    };
    for (var i = 0; i < arguments.length; i++) {
        var v = arguments[i];
        if (v !== null && v !== undefined) {
            if (v.constructor === Number) {
                args.Number.push(v);
            } else if (v.constructor === String) {
                args.String.push(v);
            } else if (v.constructor === Boolean) {
                args.Boolean.push(v);
            } else if (v.constructor === Date) {
                args.DateTime.push(v);
            } else if (v.constructor === Array) {
                args.Array.push(v);
            } else if (v.constructor === Function) {
                args.Function.push(v);
            } else if (v instanceof jQuery) {
                args.JQuery.push(v);
            } else if ("HTMLElement" in window ? v instanceof HTMLElement : v.nodeType === 1) {
                args.Dom.push(v);
            } else if ($.isPlainObject(v)) {
                args.Json.push(v);
            }
        }
    }
    return args;
};

//动态加载文件
$kk.include = function (file) {
    var option = $kk.parseArgs.apply(window, arguments);
    var files = option.Array[0] || option.String;
    var callback;
    var length = files.length;
    var ok = 0;
    if (option.Function.length > 0) {
        callback = function () {
            ok++;
            if (ok == length) {
                option.Function[0]();
            }
        };
    }

    for (var i = 0; i < length; i++) {
        var name = $.trim(files[i]);
        var att = name.split('.');
        var ext = att[att.length - 1].toLowerCase();
        if (ext == "css") {
            $("<link />").attr({ type: "text/css", rel: "stylesheet", "href": name }).appendTo("head");
            ok++;
        } else {
            $.getScript(name, callback);
        }
    }
};

//表单项渲染
$kk.drawing = function (selector) {
    if ($kk.config.stopDrawing) {
        return;
    }
    selector = $(selector || "body");
    if (selector.length == 0) {
        return;
    }
    $kk.destroy(selector);
    var filter = function (type, func) {
        var jq = selector.filter(type).add(selector.find(type));
        if (jq.length > 0) {
            func(jq);
        }
    };
    var include = function (arg, files) {
        files.push(function () {
            arg.callee.apply(arg.callee.caller, arg);
        });
        $kk.include.apply(window, files);
    };

    //a
    filter("a:not([href]),a[href=#]", function (jqs) {
        jqs.prop("href", "javascript:void(0)");
    });
    //button
    filter("button", function (jqs) {
        jqs.addClass("btn btn-default");
    })

    //icheck
    filter(":checkbox:not([draw])", function (jqs) {
        if (jqs.iCheck === undefined) {
            include(arguments, [$kk.data.icheck.js]);
            return;
        }

        jqs.each(function () {
            var jq = $(this);
            jq.iCheck($kk.getElementArgs(jq, $kk.data.icheck));
            jq.on('ifChanged', function (event) {
                this.event_ifChanged = true;
                $kk.fireEvent(this, "change");
                delete this.event_ifChanged;
            });
            jq.off("change.kk_drawing");
            jq.on("change.kk_drawing", function () {
                if (this.event_ifChanged)
                    return;
                $(this).iCheck("update");
            });
        });
    });
    //switch
    filter(":checkbox[draw=switch]", function (jqs) {
        if (jqs.bootstrapSwitch === undefined) {
            include(arguments, [$kk.data["switch"].js]);
            return;
        }
        jqs.each(function () {
            var jq = $(this);
            var options = $kk.getElementArgs(jq);
            options.onSwitchChange = function (e, data) {
                this.event_Switch = true;
                $kk.fireEvent(this, "change");  //触发click事件
                delete this.event_Switch;
            };
            jq.bootstrapSwitch(options);
            jq.off("change.kk_drawing");
            jq.on("change.kk_drawing", function () {
                if (this.event_Switch)
                    return;
                $(this).bootstrapSwitch('setState', this.checked);
            });
        });
    });
    //select
    filter("select", function (jqs) {
        jqs.each(function () {
            var jq = $(this);
            var dd = jq.data("link_dropdown"); //取出特定数据中的jquery对象(以下称为dd)
            if (dd) dd.remove();                //如果存在,则 dd 从页面中删除
            dd = $('<div class="btn-group">');  //创建 新的dd
            jq.data("link_dropdown", dd);       //将 dd 存入select.data("link_dropdown"),以便下次创建时删除

            //按bootstarp规则创建下拉框元素
            var btn = $('<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">');
            btn.text(jq.find(":selected").text()).append('<span class="caret">'); //显示被选中的元素的text,并加入'三角形'元素
            btn.width(jq.width());      //将宽度设置和原先的select元素相同
            dd.append(btn);             //添加到dd中
            var ul = $('<ul class="dropdown-menu">');
            dd.append(ul);

            var ops = jq.find("option");//获取select中所有的option元素
            for (var i = 0; i < ops.length; i++) {
                var a = $("<a href='javascript:void(0)'>" + $(ops[i]).text() + "</a>"); //设置可点击的a标签
                a.on("click", null, $(ops[i]), function (e) { //设置li元素的click事件,并将对应的option传入事件的e.data中(第三个参数)
                    var me = $(this);
                    me.parents("ul").prev().text(me.text()).append('<span class="caret">'); //设置btn的text值为点击的li的text
                    e.data.prop("selected", true); //将对应的option selected属性设置为true
                })
                ul.append($("<li>").append(a));
            }
            jq.hide();      //隐藏原有的select元素
            jq.after(dd);       //将dd插入到select元素后面
            if (!jq.data("bind_change_event")) { //判断select元素是否已经绑定了change事件
                jq.on("change", function (e) {
                    var dd = $(this).data("link_dropdown"); //获取select对应的dd
                    if (dd) { //判断dd是否已经被删除
                        var index = $(this).find(":selected").index($(this));//得到select中选中的option的索引
                        dd.find("a").eq(index).click(); //在dd中选中和option相同索引的a标签,并触发click事件
                    }
                });
                jq.data("bind_change_event", true); //将绑定change标识设置为true
            }
        })
    });
};

//销毁渲染效果
$kk.destroy = function (selector) {
    selector = $(selector || "body");
    if (selector.length == 0) {
        return;
    }

    var filter = function (type, func) {
        var jq = selector.filter(type).add(selector.find(type));
        if (jq.length > 0) {
            func(jq);
        }
    };
    //icheck
    filter(":checkbox:not([draw])", function (jq) {
        if (jq.iCheck) {
            jq.iCheck("destroy");
            jq.off("change.$kk_icheck");
        }
    });
    //switch
    filter(":checkbox[draw=switch]", function (jq) {
        if (jq.bootstrapSwitch)
            jq.bootstrapSwitch('destroy');
    });
    //select
    filter("select", function (jq) {
        jq.each(function () {
            var dd = $(this).data("link_dropdown");
            if (dd) {
                $(this).show();
                dd.remove();
            }
        });
    });


};

//弹出层
$kk.popup = function (message, delay, type) {
    var config = $kk.parseArgs(delay, type);
    config = $kk.mergePlainObject({
        message: message,
        delay: config.Number[0],
        type: config.String[0]
    }, config.Json[0]);

    var panel = $("div[alert-panel]");
    if (panel.length == 0) {
        panel = $("<div alert-panel style='position:fixed;z-index:1000;top:0;left:0;width:100%;text-align: center;' />");
        $("body").prepend(panel);
    }
    var $div = $("<div onselectstart='return false' class='alert alert-dismissible'/>")

    $div.click(function () { $(this).slideUp(function () { $(this).remove(); }); });
    config.type = {
        warning: "warning",
        warn: "warning",
        danger: "danger",
        error: "danger",
        err: "danger",
        success: "success",
        ok: "success",
        info: "info"
    }[config.type] || "info";
    $div.addClass("alert-" + config.type);
    $div.css({ padding: "7px 10px 5px", "margin-bottom": "5px", cursor: "pointer" });
    $div.append(config.message).hide();
    panel.prepend($div);
    $div.slideDown();
    if (config.delay > 0 || (config.type == "success" && config.delay == null)) {
        setTimeout(function () { $div.slideUp(function () { $(this).remove(); }); }, config.delay || 1500);
    }
    return $div;
};

//对话框
$kk.dialog = function (message, title, config, isModal, dom) {
    if ($.isPlainObject(message)) {
        return dialog(message);
    }
    var args = $kk.parseArgs(title, config, isModal, dom);
    config = args.Json[0] || {};
    if (args.String.length > 0) {
        config.title = args.String[0];
    }
    config.content = message;
    config = $kk.mergePlainObject(config, { fixed: true });
    var maximize = config.maximize;
    delete config.maximize;
    if (config.id && config.id == "$kk-loading" && $kk.isLoading == false) {
        return;
    }
    config.zIndex = $kk.config.loadingZindex;
    var dia = dialog(config);
    if (config.id == "$kk-loading" && $kk.isLoading == false) {
        dia.remove();
        return;
    }
    if (maximize) {
        $(dia.node).find("[i=header]").dblclick(function () {
            if (!dia.layout || dia.layout.state != "max") {
                dia.maximize();
            } else {
                dia.restore();
            }
        });
    }
    if (args.Boolean[0] === true) {
        return dia.showModal(args.Dom[0]);
    } else {
        return dia.show(args.Dom[0]);
    }
};

//模态对话框
$kk.alert = function (message, title) {
    return this.dialog(message, title, true, { cancel: false, quickClose: true });
};

//是否处于loading状态
$kk.isLoading = false;
$kk.timer = null;
//显示loading框
$kk.loading = function (delayParam) {
    var delay = delayParam || this.config.loadingDelay;
    var id = "$kk-loading";
    if (this.isLoading) {
        return false;
    }
    $kk.isLoading = true;
    $("body").append("<div shade='shade' routePart='true' style='position:absolute;width:100%;height:100%;top:0;left:0;' />").css("position", "relative"); //loding前遮罩
    var config = { id: id, height: "40px", cancel: function () { return false; }, cancelDisplay: false };
    var dialog = this.dialog;
    $kk.timer = setTimeout(function () { dialog(undefined, true, config); }, delay);
    return true;
};

//关闭loading框
$kk.unLoading = function () {
    $("body > div[shade='shade']").remove();
    var id = "$kk-loading";
    clearTimeout($kk.timer);
    if (this.isLoading) {
        $kk.isLoading = false;
        if ("dialog" in window) {
            var x = dialog.get(id);
            x && x.remove();
        }
        return true;
    }
    return false;
}

//打开一个url
$kk.openUrl = function (url, title, isModal, width, heigth) {
    var args = this.parseArgs(title, isModal, width, heigth);
    return this.dialog(undefined, { url: url, padding: 10, title: args.String[0] || " ", width: args.Number[0], height: args.Number[1], maximize: true }, args.Boolean[0]);
}

//对话框
$kk.confirm = function (message, title, yes, no, yesFunc, noFunc) {
    var args = this.parseArgs(yes, no, yesFunc, noFunc);
    var config = {};
    config.ok = args.Function[0];
    config.cancel = args.Function[1] || true;
    config.okValue = args.String[0] || "确定";
    config.cancelValue = args.String[1] || (config.ok ? "取消" : "关闭");
    return $kk.dialog(message, title, true, config);
};

//填充table数据
$kk.fillTable = function (selector, data, setData) {
    var table = $(selector)
    if (table.length != 1 || table.is("table") == false) {
        throw Error("绑定对象太多或不是table");
    }

    var func = null;
    if (setData && setData.constructor === Function) {
        func = setData;
    }

    var template = table.data("grid-template");
    if (!template) {
        template = table.find("tr[template]");
        if (template.length == 0) {
            template = table.find("tbody tr:first");
        }
        table.data("grid-template", $("<div />").append(template.attr("data-row", "").show()).html());
        template.remove();
        template = table.data("grid-template");
    } else {
        table.find("[data-row]").remove();
    }
    if (!data || data.constructor !== Array) {
        return;
    }

    var tbody = table.find("tbody");
    for (var i in data) {
        var it = data[i];
        var html = template.replace(/\{[^{}]+\}/g, function (a) {
            var name = a.slice(1, -1);
            if (func) {
                var value = func(it, name);
                if (value !== undefined) {
                    return $kk.IsNull(value);
                }
            }
            return $kk.encodeHTML(it[name]);
        });
        tbody.append(html);
    }
};

//填充除table以外的循环数据
$kk.fillList = function (selector, data, setData) {
    var list = $(selector)
    if (list.length != 1) {
        throw Error("绑定对象太多");
    }

    var func = null;
    if (setData && setData.constructor === Function) {
        func = setData;
    }

    var template = list.data("grid-template");
    if (!template) {
        template = list.find("[template]");
        if (template.length == 0) {
            template = list.find("> *");
        }
        list.data("grid-template", $("<div />").append(template.attr("data-item", "").show()).html());
        template.remove();
        template = list.data("grid-template");
    } else {
        list.find("[data-item]").remove();
    }
    if (!data || data.constructor !== Array) {
        return;
    }

    for (var i in data) {
        var it = data[i];
        var html = template.replace(/\{[^{}]+\}/g, function (a) {
            var name = a.slice(1, -1);
            if (func) {
                var value = func(it, name);
                if (value !== undefined) {
                    return $kk.IsNull(value);
                }
            }
            return $kk.encodeHTML(it[name]);
        });
        list.append(html);
    }
};

//填充不循环的数据
$kk.fillData = function (selector, data, setData) {
    var panel = $(selector)
    if (panel.length == 1 && panel.is("table")) {
        $kk.fillTable(selector, data, setData);
        return;
    } else if (panel.length == 0) {
        return;
    } else if (panel.length > 1) {
        panel.each(function () {
            $kk.fillData(this, data, setData);
        });
        return;
    }
    var func = null;
    if (setData && setData.constructor === Function) {
        func = setData;
    }
    var template = panel.data("grid-template");
    if (!template) {
        template = panel.html();
        panel.data("grid-template", template);
    } else {
        panel.html('');
    }
    if (data) {
        var html = template.replace(/\{[^{}]+\}/g, function (a) {
            var name = a.slice(1, -1);
            if (func) {
                var value = func(data, name);
                if (value !== undefined) {
                    return $kk.IsNull(value);
                }
            }
            return $kk.encodeHTML(data[name]);
        });
        panel.html(html);
    }
};

$(document).on("change.kk_bindCheckAll", ":checkbox", function (e) {
    var flag = $(this).attr("kk_checkflag");
    if (flag) {
        function setChecked(ckb, b) {
            if (ckb.checked !== b) {
                ckb.checked = b;
                $kk.fireEvent(ckb, "change");  //触发click事件
            }
        };

        var first = flag.charAt(0);
        flag = flag.substr(1);
        if (kk.data[flag]) {
            return;
        }
        kk.data[flag] = true;
        if (first == "m") {
            var checked = $(this).prop("checked");
            $(":checkbox[kk_checkflag='s" + flag + "']").each(function () {
                setChecked(this, checked);
            });
        } else {
            setChecked(
                $(":checkbox[kk_checkflag='m" + flag + "']")[0],
                $(":checkbox[kk_checkflag='s" + flag + "']").not(":checked").length == 0
                );
        }

        delete kk.data[flag];
    }
});
//绑定全选事件
$kk.bindCheckAll = function (checkbox, checkboxs) {

    checkbox = $(checkbox);
    if (checkbox.length < 1) {
        this.error("至少需要1个元素");
        return;
    }
    checkboxs = $(checkboxs).not(checkbox);
    if (checkboxs.length < 1) {
        return;
    }
    var flag = Math.random();
    checkbox.attr("kk_checkflag", "m" + flag);
    checkboxs.attr("kk_checkflag", "s" + flag);
};

//树状函数
$kk.drawingTree = function (tds) {

    var tree = $.map(tds, function (n) {
        return parseInt($(n).attr("level"));
    });

    var createNode = function (index) {
        var curr = {
            lv: tree[index],
            icons: []
        };
        tree[index] = curr;
        if (curr.lv == 0) { //顶级菜单
            return curr;
        }

        for (var i = 0; i < (curr.lv - 1) ; i++) {
            curr.icons.push(0);
        }

        if (tree.length == index + 1) { //最后一个菜单
            curr.icons.push(2);
            return curr;
        }

        var next = tree[index + 1];
        var diff = next.lv - curr.lv;

        var copyIcons = function (count) {
            count = Math.min(count, next.icons.length);
            for (var i = 0; i < count; i++) {
                curr.icons[i] = (next.icons[i] == 0) ? 0 : 1;
            }
        }

        switch (diff) {
            case 0:
                copyIcons(curr.lv - 1);
                curr.icons.push(3);
                break;
            case 1:
                copyIcons(curr.lv - 1);
                var icon = next.icons.slice(-2, -1)[0];
                switch (icon) {
                    case 0:
                        curr.icons.push(2);
                        break;
                    case 1:
                        curr.icons.push(3);
                        break;
                    case 2:
                        curr.icons.push(1);
                        break;
                    case 3:
                        curr.icons.push(3);
                        break;
                    default:
                        break;
                }
                break;
            case -1:
                copyIcons(curr.lv - 2);
                if (next.lv > 0) {
                    curr.icons.pop();
                    curr.icons.push(1);
                }
                curr.icons.push(2);
                break;
            default:
                copyIcons(curr.lv - 1);
                curr.icons.push(2);
                break;
        }
        return curr;
    }

    for (var i = tree.length - 1; i >= 0; i--) {
        var td = $(tds[i]);
        var node = createNode(i);
        if (node.lv == 0) {
            continue;
        }
        var txt = td.html();
        td.empty();
        for (var j in node.icons) {
            switch (node.icons[j]) {
                case 0:
                    td.append('<div class="line_none fl">');
                    break;
                case 1:
                    td.append('<div class="line_conn fl">');
                    break;
                case 2:
                    td.append('<div class="line_bottom fl">');
                    break;
                case 3:
                    td.append('<div class="line_center fl">');
                    break;
                default:
                    break;
            }
        }
        td.find("div:last-child").html(txt);
    }
};

$kk.getStyle = function(element,attr){
    if(typeof window.getComputedStyle!='undefined'){
        return parseInt(window.getComputedStyle(element,null)[attr]);           
    }else if(element.currentStyle){
        return parseInt(element.currentStyle[attr]);       
    }
};

$kk.getRoute = function(){
    try{
        var urlString = location.href.split("?")[1].split("#")[0],obj = {};
        var a = urlString.split("&");
        for(var i=0;i<a.length;i++){
            var m = a[i].split("=");
            obj[m[0]] = m[1];
        }
    }catch(e){
        console.log(e);
    }
    return obj;
}

$kk.module = function(selector,url,extraData,callback){
    $.ajax({
        url : url,
        type : "get",
        success : function(data){
            $(selector).append(data).children().data("extraData",extraData);
            try{callback()}catch(e){}
            return false;
        },
        error : function(e,x,s,r){
            console.log(arguments);
        }
    })
}

$kk.drawData = function (html, field) {
    function replace(scope){
        var count=0;
        for(var k in scope){
            if(k!=="isJsonpComplete"){count++;}
        }
        if(count==0){return html}
        html = html.replace(/\@![^!@@!]+\!@/g, function (a) {
            var name = a.slice(2, -2);
            if(name.indexOf(".")>=0){
                var arr = name.split("."),src=false,l=0;
                while(l<arr.length){
                    if($.isNumeric(src[arr[l]]) || $.isNumeric(scope[arr[l]])){
                        src =  src[arr[l]].toString() || scope[arr[l]].toString();
                    }else{
                        src = src[arr[l]] || scope[arr[l]] || "";
                    }
                    l++;
                }
                return $kk.encodeHTML(src);
            }else{
                return $kk.encodeHTML(scope[name]);
            }
        });
    }
    if($.isArray(field)){
        var chtml = "";
        for(var i=0;i<field.length;i++){
            template = replace(field[i]);
            chtml+=html;
        }
        return chtml;
    }
    replace(field);
    return html;
}

$kk.isNotEmpty = function(obj){
    var i = 0;
    for(var k in obj){
        i++;
    }
    return !!i;
}

$kk.bindData = function(dom){
    var $scope = $kk.$scope;
    function getLoopData(src,arr,scope,isRoot,isDecode){
        var l=0,src={};
        if(!isRoot){
            arr = arr.slice(1);
        }
        while(l<arr.length){
            if(arr[l] in scope || arr[l] in src){
                if(l === arr.length - 1){
                    try{
                        src = src[arr[l]].toString();
                    }catch(e){
                        src = scope[arr[l]].toString();
                    }
                    break;
                }
                src = src[arr[l]] || scope[arr[l]];
                l++;
            }else{
                src = "";
                break;
            }
        }
        if(isDecode){
            return $kk.decodeHTML(src)
        }
        return $kk.encodeHTML(src);
    }
    function getTemplateParent(){
        for(var i=0;i<$(this).parents().length;i++){
            if($(this).parents().eq(i).attr("kk-template")){
                return false;
            }
        }
        return true;
    }
    var $repeatArr = dom.find("[kk-repeat]").filter(function(){
        return $(this).attr("kk-template");
    });
    $repeatArr.each(function(){
        var arr = $(this).attr("kk-repeat").split("in");
        var scopeArr = $.trim(arr[1]).split(".");
        var tempScope = $scope[scopeArr[0]] || [];
        if(scopeArr.length > 1 && $kk.isNotEmpty(tempScope)){
            var i = 1;
            while(i<=scopeArr.length){
                if(i == scopeArr.length){
                    break;
                }
                try{
                    tempScope = tempScope[scopeArr[i]];
                }catch(e){
                    console.log(e);
                    tempScope = [];
                    break;
                }
                i++;
            }
        }
        
        if(tempScope.length){
            tempScope[tempScope.length-1].index = tempScope.length-1;
            $(this).attr("index",tempScope.length-1).find("[kk-bind],[kk-if],[kk-attr]").andSelf().data("kk-repeat-data",tempScope[tempScope.length-1]);
            for(var i=0;i<tempScope.length;i++){
                var cloneDom = $(this).clone(true);
                cloneDom.removeAttr("kk-template").show();
                tempScope[i].index = i;
                $(this).before(cloneDom).prev().attr("index",i).find("[kk-bind],[kk-if],[kk-attr]").andSelf().data("kk-repeat-data",tempScope[i]);
            }
        }
        $(this).hide();
    })
    var $if = dom.find("[kk-if]").filter(getTemplateParent);
    $if.each(function(){
        var arr = $(this).attr("kk-if").split("=="),src="";
        var scopeArr = $.trim(arr[0]).split(".");
        var repeatData = $(this).data("kk-repeat-data");
        if(scopeArr.length == 1){
            src = $scope[$.trim(arr[0])];
        }else{
            if(repeatData){
                try{
                    src = getLoopData(src,scopeArr,repeatData,false);
                }catch(e){
                    src = getLoopData(src,scopeArr,$scope,true);
                }
            }else{
                src = getLoopData(src,scopeArr,$scope,true);
            }
        }
        if(src !== $.trim(arr[1]).substring(1,$.trim(arr[1]).length-1)){
            $(this).hide();
        }else{
            $(this).show();
        }
    })
    
    var $attr = dom.find("[kk-attr]").filter(getTemplateParent);
    $attr.each(function(){
        var attrArr = $(this).attr("kk-attr").split(",");
        for(var i=0;i<attrArr.length;i++){
            var tempJson = JSON.parse("{" + attrArr[i] + "}");
            for(var k in tempJson){
                var arr = tempJson[k].split(".");
            }
            var src = "";
            var repeatData = $(this).data("kk-repeat-data");
            if(arr.length==1){
                src = $scope[arr[0]];
            }else{
                if(repeatData){
                    try{
                        src = getLoopData(src,arr,repeatData,false,true);
                    }catch(e){
                        src = getLoopData(src,arr,$scope,true,true);
                    }
                }else{
                    src = getLoopData(src,arr,$scope,true,true);
                }
            }
            if($(this).attr("kk-replaceValue") == "true"){
                $(this).attr("kk-attr-"+k,src);
            }else{
                $(this).attr(k,src);
				$(this)[0][k] = src;//对于用户输入的value改变
            }
        }
    })
    
    var $bind = dom.find("[kk-bind]").filter(getTemplateParent);
    $bind.each(function(){
        var arr = $(this).attr("kk-bind").split(".");
        var src = "";
        var repeatData = $(this).data("kk-repeat-data");
        if(arr.length==1){
            src = $scope[arr[0]];
        }else{
            if(repeatData){
                try{
                    src = getLoopData(src,arr,repeatData,false);
                }catch(e){
                    src = getLoopData(src,arr,$scope,true);
                }
            }else{
                src = getLoopData(src,arr,$scope,true);
            }
        }
        if($(this).attr("kk-replaceDom")){
            $(this).html(src);
        }else{
            $(this).html(src).replaceWith($(this).html() || " ");
        }
    })
}

$kk.fillData_new = function(parent){
    var $p = $(parent);
    $p.children("[kk-template=true]").siblings().remove();
    $kk.bindData($p);
    $(parent).replaceWith($p);
}
$kk.route = function(cf){
    var config = cf,flag = true,$scope = {},$routeWapper = $("#routeWrapper"),$routeInner,timer;
    function childAutoHeight(){
        var _height = $kk.getStyle($routeInner[0],"height");
        var outHeight = $kk.getStyle($routeWapper[0],"height") - parseInt($routeInner.css("margin-bottom"))*2;
        if(_height < outHeight){
            $routeInner.css("min-height",outHeight);
        }else{
            //$routeInner.css("height",_height);
        }
    }
    function resize(){
        var _width = $(window).width() + "px";
        var _height = $(window).height() + "px";
        $routeWapper.css({width : _width,height:_height,"overflow-x":"auto","overflow-y":"auto"});
    }
    var success = function(data){
        html = $kk.drawData(data,$scope);
        var fdom = $(html);
        $kk.bindData(fdom,$scope);
        $routeWapper.empty().append(fdom).siblings().filter(function(){
            return !$(this).attr("routepart") && $(this)[0].tagName.toLowerCase() != "script";
        }).remove();
        $routeInner=$("#routeWrapper>div");
        resize();
        childAutoHeight();
    }
    function pathRegExp(path) {
        var insensitive = false,
            ret = {
              originalPath: path,
              regexp: path
            },
            keys = ret.keys = [];
        path = path
          .replace(/([().])/g, '\\$1')
          .replace(/(\/)?:(\w+)([\?\*])?/g, function(_, slash, key, option) {
            var optional = option === '?' ? option : null;
            var star = option === '*' ? option : null;
            keys.push({ name: key, optional: !!optional });
            slash = slash || '';
            return ''
              + (optional ? '' : slash)
              + '(?:'
              + (optional ? slash : '')
              + (star && '(.+?)' || '([^/]+)')
              + (optional || '')
              + ')'
              + (optional || '');
          })
          .replace(/([\/$\*])/g, '\\$1');
        ret.regexp = new RegExp('^' + path + '$', insensitive ? 'i' : '');
        return ret;
    }
    /*对于jsonp确保加载完成*/
    function isJsonpComplete(param){
        param.isJsonpComplete = true;
        $kk.$scope = $scope;
    }
    function getView(){
        var urlObj = {},timer;
        function view(nowhash){
            $.ajax({
                url : nowhash.template,
                method : "get",
                async : true,
                success : function(data){
                    success(data);
                }
            })
        }
        function getNowHash(){
            var hash = location.hash.split("?")[0];
            for(var i=0;i<config.length;i++){
                if((config[i].url || config[i].url=="") && pathRegExp(config[i].url).regexp.test(hash)){
                    urlObj = config[i];break;
                }else{
                    urlObj = config[config.length-1];
                }
            }
        }
        getNowHash();
        try{
            urlObj.resolve($scope,isJsonpComplete)
        }catch(e){
            isJsonpComplete($scope);
        }
        timer = setInterval(function(){
            if($scope.isJsonpComplete){
                getNowHash();
                view(urlObj);
                clearInterval(timer);
            }
        },13);
    }
    /*绑定change事件*/
    $(window).hashchange(function(){
        $scope = {};
        getView();
    })
    resize();
    $(window).resize(function(){
        resize();
    })
    /*第一次带有#路由,或者不带#*/
    if(!location.hash.length || (location.hash.length && flag)){
        getView();
    }
}

$kk.destroyFile = function(filename, filetype){
    var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none"
    var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none"
    var allsuspects=document.getElementsByTagName(targetelement)
    for (var i=allsuspects.length; i>=0; i--){
    if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)
       allsuspects[i].parentNode.removeChild(allsuspects[i])
    }
}

//格式化json对象或json字符串
$kk.jsonFormat = function (json) {
    var obj = json;
    if (json.constructor === String) {
        obj = JSON.parse(json);
    }

    var buffer = [];

    var space = function (c) {
        for (var i = 0; i < c; i++) {
            buffer.push("&nbsp;");
        }
    }

    var append = function (o, d) {
        if ($.isArray(o)) {
            buffer.push("[<br />");
            var b = false;
            for (var i in o) {
                if (b) {
                    buffer.push(" ,<br />");
                }
                b = true;
                space((d + 1) * 2);
                append(o[i], d + 1);
            }
            buffer.push("<br />");
            space(d * 2);
            buffer.push("]");
        } else if ($.isPlainObject(o)) {
            buffer.push("{<br />");
            var b = false;
            for (var i in o) {
                if (b) {
                    buffer.push(" ,<br />");
                }
                b = true;
                space((d + 1) * 2);
                buffer.push(JSON.stringify(i));
                buffer.push(" : ");
                append(o[i], d + 1);
            }
            buffer.push("<br />");
            space(d * 2);
            buffer.push("}");
        } else {
            buffer.push(JSON.stringify(o));
        }
    }
    append(obj, 0);
    return buffer.join("");
};

//获取url参数值
$kk.queryString = function (name) {
    var rex = new RegExp("[?&]\s*" + name + "\s*=([^&$#]*)", "i");
    var r = rex.exec(location.search);
    if (r && r.length == 2)
        return decodeURIComponent(r[1]);
}

$kk.IsNull = function (obj, defaultValue) {
    if (obj != null) {
        return obj;
    }
    if (defaultValue == null) {
        return "";
    }
    return defaultValue;
}

$kk.encodeHTML = function (data) {
    if (data == null) {
        return "";
    }
    return data.toString().replace(/[<>&" ]/g, function (m) {
        switch (m) {
            case "<":
                return "&lt;";
            case ">":
                return "&gt;";
            case "&":
                return "&amp;";
            case '"':
                return "&quot;";
            case " ":
                return "&nbsp;";
            default:
                break;
        }
    })
}

$kk.decodeHTML = function (data) {
    if (data == null) {
        return "";
    }
    var temp = document.createElement("div");
    temp.innerHTML = data;
    var output = temp.innerText || temp.textContent;
    temp = null;
    return output;
}

$kk._api = function (url) {
    var me = this;
    me.baseUrl = url;

    function processResult(result, success, fail) {
        if ("Code" in result && "Message" in result && "Data" in result) {
            if (result.Code == 1) {
                kk.alert(result.Message);
                return;
            }
            if (result.Code == 3) {
                document.write(result.Message);
                return;
            }
            if (result.Code == 302) {
                kk.loading();
                window.location = result.Message + window.location.hash;
                return;
            }
            if (result.Code == 4002) {
                kk.loading();
                if ($kk.config.login.url) {
                    var loginUrl = $kk.urlCombine(me.baseUrl, $kk.config.login.url);
                    if(window.location.hash.length > 0){
                        var _hash = window.location.hash;
                        var _path = !!window.location.search.length ? "&_referer=" : "?_referer=";
                        loginUrl += _path + window.location.hash;
                    }
                    $kk.config.login.target.location = loginUrl;
                } else {
                    kk.popup("用户为登陆", "err");
                }
                return;
            }
            if (result.Code != 0) {
                if (fail) {
                    fail(result);
                } else {
                    kk.popup(result.Message, "err");
                }
                return;
            }
            result = result.Data;
        }
        try {
            success && success(result);
        } catch (e) {
            $kk.error("回调方法异常:" + e.message);
        }
    };

    function ajax(method, route, data, success, fail, async) {
        if ($.isFunction(data)) {
            if ($.isFunction(success)) {
                fail = success;
            }
            success = data;
            data = null;
        }
        data = data || {};
        data.agent = "ajax";
        data.uk || (data.uk = $kk.queryString("uk"));
        data._r_ = Math.random();
        //$kk.loading();
        try{
            var isasync = async.toString() === "false" ? false : true;
        }catch(e){
            var isasync = true
        }
        return $.ajax({
            dataType: "json",
            url: $kk.urlCombine(me.baseUrl, route),
            type: method,
            async : isasync,
            data: data,
            success: function (r) {
                processResult(r, success, fail);
            }
        });
    }
    this.get = function (route, data, success, fail, async) {
        return ajax("GET", route, data, success, fail, async);
    }
    this.post = function (route, data, success, fail, async) {
        return ajax("POST", route, data, success, fail, async);
    }
    this.jsonp = function (route, data, success, fail) {
        if ($.isFunction(data)) {
            if ($.isFunction(success)) {
                fail = success;
            }
            success = data;
            data = null;
        }
        data = data || {};
        data.agent = "jsonp";
        data.uk || (data.uk = $kk.queryString("uk"));
        //kk.loading();
        
        var result = $.ajax({
            dataType: "jsonp",
            url: $kk.urlCombine(me.baseUrl, route),
            type: "POST",
            data: data,
            success: function (result) {
                if(result.isTimeout == null){
                    kk.unLoading();
                    processResult(result, success, fail);
                }
            }
        });
        setTimeout(function () {
            result.isTimeout = true;
            if (result.readyState == 1) {
                if(fail){
                    fail();
                }else{
                    kk.popup("请求超时:" + $kk.urlCombine(me.baseUrl, route), "err");
                }
                $kk.unLoading();
            }
        }, $kk.config.jsonpTimeout);
    }
};
(function () {
    $kk.api = new $kk._api(window.location.origin || (window.location.protocol + "//" + window.location.host));
})();
$kk.api.add = function (name, url) {
    this[name] = new $kk._api(url);
};

//OAuth授权
$kk.oauth = function (urls) {
    if (urls) {
        kk.api.jsonp("/oauth/activeauth", { urls: urls }, function (data) {
            for (var i in data) {
                var url = data[i];
                if (url.charAt(0) == '[') {
                    kk.error(url);
                } else {
                    kk.api.jsonp(data[i]);
                }
            }
        });
    }
}


//处理url
$kk.urlCombine = function (url, route) {
    if (route == null) {
        return url;
    }
    if (route.indexOf('://') > 0) {
        return route;
    }
    if (route.charAt(0) == '/') {
        var start = url.indexOf("://") + 3;
        var length = url.substring(start).indexOf("/");
        if (length > 0) {
            var domain = url.substring(0, start + length);
            return domain + route;
        }
        return url + route;
    }
    if (url.slice(-1) != '/') {
        return url + "/" + route;
    }
    return url + route;
}

//其他功能
//格式化时间
var ___DateToString = Date.prototype.toString;
Date.prototype.toString = function (format) {
    if (format === undefined) {
        return ___DateToString.apply(this);
    }
    if (format === null) {
        format = "yyyy-MM-dd HH:mm:ss";
    }

    format = format.replace(/yyyy/g, this.getFullYear());
    format = format.replace(/yyy/g, this.getYear());
    format = format.replace(/yy/g, this.getFullYear().toString().slice(-2));

    if (format.indexOf('mi') >= 0) {
        format = format.replace(/mi/g, this.getMilliseconds().toString());
    }

    if (format.indexOf('M') >= 0) {
        var M = (this.getMonth() + 1).toString();
        format = format.replace(/MM/g, ("0" + M).slice(-2));
        format = format.replace(/M/g, M);
    }

    if (format.indexOf('ddd') >= 0) {
        var xq = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"];
        format = format.replace(/ddd/g, xq[this.getDay()]);
    }

    if (format.indexOf('d') >= 0) {
        var d = this.getDate().toString();
        format = format.replace(/dd/g, ("0" + d).slice(-2));
        format = format.replace(/d/g, d);
    }


    if (format.indexOf('h') >= 0 || format.indexOf('H') >= 0) {
        var h = this.getHours().toString();
        format = format.replace(/HH/g, ("0" + h).slice(-2));
        format = format.replace(/H/g, h);
        h = h % 12;
        format = format.replace(/hh/g, ("0" + h).slice(-2));
        format = format.replace(/h/g, h);
    }

    if (format.indexOf('m') >= 0) {
        var m = this.getMinutes().toString();
        format = format.replace(/mm/g, ("0" + m).slice(-2));
        format = format.replace(/m/g, m);
    }

    if (format.indexOf('s') >= 0) {
        var s = this.getSeconds().toString();
        format = format.replace(/ss/g, ("0" + s).slice(-2));
        format = format.replace(/s/g, m);
    }

    if (format.indexOf('f') >= 0) {
        var f = this.getMilliseconds().toString();
        format = format.replace(/fff/g, ("000" + f).slice(-3));
        format = format.replace(/ff/g, ("000" + f).slice(-3).substr(0, 2));
        format = format.replace(/f/g, ("000" + f).slice(-3).substr(0, 1));
    }
    //.getMilliseconds()

    return format;
};
Date.prototype.checkNumber = function (value) {
    if (+value !== value) {
        throw new Error("必须是数字");
    }
};

//时间加减
Date.prototype.add = function (type, value) {
    this.checkNumber(value);
    var date = new Date(this);
    switch (type) {
        case 'y':
        case 'Y':
            date.setYear(this.getYear() + value);
            break;
        case 'M':
            date.setMonths(this.getMonths() + value);
            break;
        case 'd':
        case 'D':
            date.setDate(this.getDate() + value);
            break;
        case 'h':
        case 'H':
            date.setHours(this.getHours() + value);
            break;
        case 'm':
            date.setMinutes(this.getMinutes() + value);
            break;
        case 'S':
        case 's':
            date.setSeconds(this.getSeconds() + value);
            break;
        case 'f':
            date.setMilliseconds(this.getMilliseconds() + value);
            break;
        default:
            throw Error("type只能是:y,Y,M,d,D,h,H,m,s,S,f");
    }
    return date;
};

Date.prototype.addDays = function (value) { return this.add("d", value); };
Date.prototype.addHours = function (value) { return this.add("h", value); };
Date.prototype.addMilliseconds = function (value) { return this.add("f", value); };
Date.prototype.addMinutes = function (value) { return this.add("m", value); };
Date.prototype.addMonths = function (value) { return this.add("M", value); };
Date.prototype.addSeconds = function (value) { return this.add("s", value); };
Date.prototype.addYears = function (value) { return this.add("y", value); };


//ajax全局处理
jQuery(document).ajaxStart(function () {
    //$kk.loading();
});
jQuery(document).ajaxStop(function () {
    $kk.unLoading();
});
jQuery(document).ajaxError(function (event, result, ajax) {
    if (result.responseText && result.responseText.substr(0, 12) == "error_debug:") {
        $kk.confirm(result.responseText, "DEBUG");
    } else {
        $kk.popup("请求服务器: " + ajax.url + " 失败\n原因: " + result.statusText, "err");
    }
});

$(window).on("beforeunload", function () {
    if (navigator || (/(iPhone|iPad|iPod)/i).test(navigator.userAgent) == false) { //判断safari浏览器
        //kk.loading(); //页面跳转时的loading效果
    }
});

//拓展jquery方法
jQuery.prototype.attrop = function (name, value) {
    if (value) {
        this.attr(name, value);
        this.prop(name, value);
        return this;
    } else {
        return this.attr(name) || this.prop(name);
    }
}
//处理页面所有a标签
$("a:not([href]),a[href=#]").attrop("href", "javascript:void(0)");